//
//  MacGCRTests.mm
//  Clock SignalTests
//
//  Created by Thomas Harte on 15/06/2019.
//  Copyright © 2019 Thomas Harte. All rights reserved.
//

#import <XCTest/XCTest.h>

#include "Storage/Disk/Encodings/AppleGCR/Encoder.hpp"
#include "Storage/Disk/Track/TrackSerialiser.hpp"
#include "Storage/Disk/Encodings/AppleGCR/SegmentParser.hpp"

@interface MacGCRTests : XCTestCase
@end

@implementation MacGCRTests {
}

- (void)testSector0Track0Side0Header {
	const auto header = Storage::Encodings::AppleGCR::Macintosh::header(0x22, 0, 0, false);
	const std::vector<uint8_t> expected_mark = {
		0xd5, 0xaa, 0x96,
		0x96, 0x96, 0x96, 0xd9, 0xd9,
		0xde, 0xaa, 0xeb
	};
	const auto mark_segment = Storage::Disk::PCMSegment(expected_mark);

	XCTAssertEqual(mark_segment.data, header.data);
}

- (void)testSector9Track11Side1Header {
	const auto header = Storage::Encodings::AppleGCR::Macintosh::header(0x22, 11, 9, true);
	const std::vector<uint8_t> expected_mark = {
		0xd5, 0xaa, 0x96,
		0xad, 0xab, 0xd6, 0xd9, 0x96,
		0xde, 0xaa, 0xeb
	};
	const auto mark_segment = Storage::Disk::PCMSegment(expected_mark);

	XCTAssertEqual(mark_segment.data, header.data);
}

- (void)testData {
	// This encoding was generated by the first version of my GCR encoder that produced data a Mac
	// would accept.
	const std::vector<uint8_t> expected_data = {
		/* Standard prologue. */
		0xd5, 0xaa, 0xad,

		/* Sector number. */
		0x96,

		/* Tags, after GCR encoding. */
		0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96, 0x96,

		/* Sector body. */
		0xb4, 0xae, 0xa6, 0xe7, 0xf6, 0x96, 0xae, 0xaf, 0xdc, 0xae, 0xcf, 0xce, 0x9d, 0xbe, 0xad, 0xed,
		0xe6, 0xd7, 0xf4, 0xd9, 0xf4, 0xfd, 0x9f, 0xdb, 0xba, 0xb7, 0xe9, 0x9d, 0x9f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d, 0xdf, 0xae, 0xb4, 0xbb, 0xdd,
		0xce, 0xcf, 0xb7, 0xfd, 0xec, 0xce, 0xbe, 0xde, 0xf2, 0xbd, 0xe7, 0x9d, 0xdf, 0xce, 0xac, 0xbf,
		0xd3, 0xfe, 0xe7, 0xdd, 0xfc, 0xb6, 0xe7, 0xb4, 0xfa, 0xbd, 0xef, 0xae, 0xa7, 0xdf, 0xbd, 0xf7,
		0xa7, 0xdd, 0xbe, 0x9e, 0x9a, 0xea, 0x97, 0xdb, 0xb2, 0x9b, 0xa7, 0xd7, 0x96, 0xf5, 0xb4, 0xbf,
		0xbc, 0xd7, 0x9a, 0xcb, 0xce, 0xb9, 0xb3, 0xb5, 0xaf, 0xb9, 0xfa, 0xf2, 0xbf, 0xb3, 0xdc, 0xdb,
		0xad, 0xd7, 0xbe, 0x9d, 0xea, 0xda, 0xb2, 0x9e, 0xea, 0xbd, 0xbe, 0xb7, 0xcb, 0xac, 0x9d, 0xb7,
		0xe7, 0xee, 0xd3, 0xb9, 0xcf, 0xce, 0xf9, 0x96, 0xf4, 0xbd, 0xab, 0xb5, 0xed, 0xe5, 0xea, 0xf4,
		0xfb, 0xd7, 0xf7, 0xbe, 0xf4, 0xfb, 0xb4, 0xbf, 0xee, 0xac, 0xde, 0xbd, 0xdc, 0xb6, 0xcf, 0xab,
		0xb7, 0xaf, 0xf7, 0xfb, 0xb3, 0xb3, 0xee, 0xe9, 0xcd, 0xb7, 0xd9, 0xfc, 0xd9, 0x9d, 0xef,

		/* Standard epilogue. */
		0xde, 0xaa, 0xeb
	};

	// This is the first sector, taken from a random disk image from the internet.
	const uint8_t source_data[524] = {
		/* Tags. */
		0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,

		/* Sector body. */
		0x4c, 0x4b, 0x60, 0x00, 0x00, 0x86, 0x00, 0x12, 0x00, 0x00, 0x06, 0x53, 0x79, 0x73, 0x74, 0x65,
		0x6d, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x06, 0x46, 0x69, 0x6e, 0x64, 0x65,
		0x72, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x07, 0x4d, 0x61, 0x63, 0x73, 0x62,
		0x75, 0x67, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0c, 0x44, 0x69, 0x73, 0x61, 0x73,
		0x73, 0x65, 0x6d, 0x62, 0x6c, 0x65, 0x72, 0x20, 0x20, 0x20, 0x0d, 0x53, 0x74, 0x61, 0x72, 0x74,
		0x55, 0x70, 0x53, 0x63, 0x72, 0x65, 0x65, 0x6e, 0x20, 0x20, 0x06, 0x46, 0x69, 0x6e, 0x64, 0x65,
		0x72, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x0e, 0x43, 0x6c, 0x69, 0x70, 0x62,
		0x6f, 0x61, 0x72, 0x64, 0x20, 0x46, 0x69, 0x6c, 0x65, 0x20, 0x00, 0x0a, 0x00, 0x14, 0x00, 0x00,
		0x43, 0x00, 0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0xc0, 0x00, 0x22, 0x38, 0x01, 0x08, 0x48, 0x41,
		0x0c, 0x41, 0x00, 0x04, 0x6e, 0x2c, 0x72, 0x00, 0x50, 0xf9, 0x00, 0x01, 0xff, 0xf0, 0x42, 0xb9,
		0x00, 0x03, 0xff, 0xf0, 0x4a, 0xb9, 0x00, 0x01, 0xff, 0xf0, 0x67, 0x16, 0x70, 0x02, 0x48, 0x40,
		0xd1, 0xb8, 0x01, 0x08, 0xd1, 0xb8, 0x08, 0x24, 0xd1, 0xb8, 0x02, 0x66, 0xd1, 0xb8, 0x01, 0x0c,
		0x72, 0x04, 0x20, 0x78, 0x02, 0xa6, 0x0c, 0x41, 0x00, 0x08, 0x6f, 0x02, 0x72, 0x08, 0xd1, 0xfb,
		0x10, 0xae, 0x2f, 0x08, 0x70, 0x07, 0x41, 0xf8, 0x0a, 0xb8, 0x42, 0x98, 0x51, 0xc8, 0xff, 0xfc,
		0x34, 0x3a, 0xff, 0x9a, 0x70, 0x16, 0xc0, 0xc2, 0x22, 0x00, 0xa7, 0x1e, 0x43, 0xf8, 0x01, 0x54,
		0x53, 0x42, 0x32, 0x82, 0x42, 0xa1, 0x42, 0xa1, 0x42, 0x61, 0x23, 0x08, 0x46, 0x58, 0x55, 0x41,
		0x66, 0xfa, 0x33, 0x3c, 0xff, 0xef, 0x42, 0x78, 0x01, 0x84, 0x72, 0xfc, 0x70, 0x0f, 0x14, 0x38,
		0x02, 0x06, 0xc0, 0x02, 0xd0, 0x40, 0x48, 0x40, 0x10, 0x02, 0xe4, 0x48, 0xc0, 0x41, 0x48, 0x40,
		0x21, 0xc0, 0x01, 0x8e, 0x70, 0x0f, 0x14, 0x38, 0x02, 0x09, 0xc0, 0x02, 0xe5, 0x48, 0x21, 0xc0,
		0x02, 0xf4, 0x10, 0x02, 0xe4, 0x48, 0xc0, 0x41, 0x21, 0xc0, 0x02, 0xf0, 0x41, 0xf8, 0x03, 0x40,
		0x72, 0x50, 0x42, 0x58, 0x51, 0xc9, 0xff, 0xfc, 0x70, 0x1e, 0xc0, 0xfa, 0xff, 0x2e, 0x32, 0x38,
		0x01, 0x08, 0xe2, 0x49, 0xc0, 0xc1, 0x54, 0x40, 0x32, 0x00, 0xa7, 0x1e, 0x21, 0xc8, 0x03, 0x4e,
		0x30, 0xc1, 0x31, 0xfc, 0x00, 0x02, 0x03, 0x4c, 0x9e, 0xfc, 0x00, 0x32, 0x20, 0x4f, 0x31, 0x78,
		0x02, 0x10, 0x00, 0x16, 0xa0, 0x0f, 0x66, 0x00, 0x01, 0xb2, 0xde, 0xfc, 0x00, 0x32, 0x43, 0xf8,
		0x0a, 0xd8, 0x41, 0xfa, 0xfe, 0x86, 0x70, 0x10, 0xa0, 0x2e, 0x55, 0x4f, 0x2f, 0x0f, 0x48, 0x78,
		0x09, 0xfa, 0x20, 0x78, 0x08, 0x10, 0x4e, 0x90, 0x30, 0x1f, 0xe6, 0x48, 0x31, 0xc0, 0x01, 0x06,
		0x08, 0x38, 0x00, 0x06, 0x02, 0x0b, 0x56, 0xf8, 0x08, 0xd3, 0xa8, 0x52, 0x43, 0xfa, 0xfe, 0x9c,
		0x76, 0x01, 0x61, 0x00, 0x01, 0x98, 0x0c, 0x44, 0x40, 0x00, 0x6e, 0x02, 0x70, 0xff, 0x3f, 0x00,
		0x66, 0x04, 0x61, 0x00, 0x01, 0xf0, 0xa8, 0x53, 0x55, 0x4f, 0x42, 0xb8, 0x0a, 0xf2, 0xa9, 0x95,
		0x4a, 0x5f, 0x6b, 0x00, 0x01, 0x56, 0x3e, 0x1f, 0x20, 0x5f, 0xa0, 0x57, 0x21, 0xf8, 0x02, 0xa6,
		0x01, 0x18, 0x59, 0x4f, 0x2f, 0x3c, 0x44, 0x53, 0x41, 0x54, 0x42, 0x67, 0xa9, 0xa0, 0x2a, 0x1f,
		0x67, 0x00, 0x01, 0x1e, 0x20, 0x45, 0x21, 0xd0, 0x02, 0xba, 0xa8, 0xfe, 0x70, 0x28, 0x61, 0x00,
	};
	const auto data = Storage::Encodings::AppleGCR::Macintosh::data(0, source_data);
	const auto expected = Storage::Disk::PCMSegment(expected_data);
	XCTAssertEqual(data.data, expected.data);
}

- (void)testDecoding {
	const uint8_t format = 0x22;
	const uint8_t track_id = 23;
	const bool is_side_two = true;

	// Prepare a test track of 8 sectors.
	Storage::Disk::PCMSegment segment;
	segment += Storage::Encodings::AppleGCR::six_and_two_sync(24);
	for(int c = 0; c < 8; ++c) {
		uint8_t sector_id = uint8_t(c);

		uint8_t sector_plus_tags[524];

		// Provide tags plus a sector body that are just the sector number ad infinitum.
		for(size_t c = 0; c < sizeof(sector_plus_tags); ++c) {
			sector_plus_tags[c] = uint8_t(sector_id + (c * 3));
		}

		// NB: sync lengths below are identical to those for
		// the Apple II, as I have no idea whatsoever what they
		// should be.

		segment += Storage::Encodings::AppleGCR::Macintosh::header(
			format ^ c,
			track_id - c,
			sector_id,
			is_side_two ^ (c & 1)
		);
		segment += Storage::Encodings::AppleGCR::six_and_two_sync(7);
		segment += Storage::Encodings::AppleGCR::Macintosh::data(sector_id, sector_plus_tags);
		segment += Storage::Encodings::AppleGCR::six_and_two_sync(20);
	}

	// Parse the prepared track to look for sectors.
	const auto decoded_sectors = Storage::Encodings::AppleGCR::sectors_from_segment(segment);

	// Assert that the proper number of sectors was found.
	XCTAssertEqual(decoded_sectors.size(), 8);

	// Assert that the sector descriptions and contents are correct.
	int sector = 0;
	for(const auto &pair: decoded_sectors) {
		XCTAssertFalse(pair.second.has_header_checksum_error);
		XCTAssertFalse(pair.second.has_data_checksum_error);

		XCTAssertEqual(pair.second.address.is_side_two, is_side_two ^ (sector & 1));
		XCTAssertEqual(pair.second.address.format, format ^ sector);
		XCTAssertEqual(pair.second.address.track, track_id - sector);
		XCTAssertEqual(pair.second.address.sector, sector);

		for(size_t c = 0; c < sizeof(pair.second.data.size()); ++c) {
			XCTAssertEqual(pair.second.data[c], uint8_t(sector + (c * 3)));
		}

		++sector;
	}
}

@end
